//
//  XSTestModel.m
//  XSDataBaseDemo
//
//  Created by 大帅 on 2023/12/19.
//

#import "XSTestModel.h"

@implementation XSTestModel

// 视频下载完成地址拼接处理
-(NSString *)localUrl {
    // 截取视频url末尾作为存储名字
    NSString *tempUrl = _url;
    if (![_url hasSuffix:@".mp4"] && [_url containsString:@"?"]) {
        // 开始截取位置
        NSRange range = [_url rangeOfString:@"?"];
        if (range.location != NSNotFound) {
            // 截取url
            tempUrl = [_url substringToIndex:range.location];
        }
    }
    NSString *finalName = [tempUrl substringFromIndex:[tempUrl rangeOfString:@"/" options:NSBackwardsSearch].location + 1];
    NSString *fileName = [NSString stringWithFormat:@"%@_%@", self.videoId, finalName];
    _localUrl = [self.localFolder stringByAppendingPathComponent:fileName];
    return _localUrl;
}

// 视频本地存储文件夹
-(NSString *)localFolder {
    return [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:XS_DOWNLOAD_FOLDER];;
}

// 时间转换为时间戳，精确到微秒
- (NSTimeInterval)getTimeStampWithDate:(NSDate *)date {
    return [[NSNumber numberWithDouble:[date timeIntervalSince1970] * 1000 * 1000] longLongValue];
}

// 检查字符串是否为空
- (BOOL)trimEmpty:(NSString *)str {
    if (!str) {
        return YES;
    }
    if ([str isKindOfClass:[NSNull class]]) {
        return YES;
    }
    NSString *trimStr = [[NSString stringWithFormat:@"%@",str] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if([trimStr isEqualToString:@"(null)"] || [trimStr isEqualToString:@"null"]) {
        return YES;
    }
    return (trimStr == nil || trimStr.length <= 0);
}

@end
